package com.baikeyang.service.activity.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baikeyang.common.model.ActivityUserFav;
import com.baikeyang.mapper.ActivityUserFavMapper;
import com.baikeyang.service.activity.IActivityUserFavService;
import com.baikeyang.service.base.impl.SuperServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 活动收藏 服务实现类
 * </p>
 *
 * @author BaiKeyang
 * @since 2020-05-24
 */
@Service
public class IActivityUserFavServiceImpl extends SuperServiceImpl<ActivityUserFavMapper, ActivityUserFav> implements IActivityUserFavService {

    @Autowired
    private ActivityUserFavMapper activityUserFavMapper;

    @Override
    public int saveActivityUserFav(ActivityUserFav userFav) {
        userFav.setCreateTime(new Date());
        userFav.setCreateUser(0L);
        userFav.setUpdateTime(new Date());
        userFav.setUpdateUser(0L);
        return activityUserFavMapper.insert(userFav);
    }

    @Override
    public int getActivityUserFavByUserIdAndActivityId(Long userId, Long activityId) {
        QueryWrapper<ActivityUserFav> queryWrapper = Wrappers.query();
        queryWrapper.eq("USER_ID", userId);
        queryWrapper.eq("ACTIVITY_ID", activityId);
        List<ActivityUserFav> activityUserFavs = activityUserFavMapper.selectList(queryWrapper);
        return activityUserFavs.size();
    }

    @Override
    public int delActivityUserFavByUserIdAndActivityId(String userId, Long activityId) {
        QueryWrapper<ActivityUserFav> queryWrapper = Wrappers.query();
        queryWrapper.eq("USER_ID", userId);
        queryWrapper.eq("ACTIVITY_ID", activityId);
        List<ActivityUserFav> activityUserFavs = activityUserFavMapper.selectList(queryWrapper);
        if(CollectionUtil.isNotEmpty(activityUserFavs)){
            return activityUserFavMapper.deleteById(activityUserFavs.get(0).getId());
        }
        return 0;
    }
}
